package com.hcc.security.enetity;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.util.CollectionUtils;

@Data
public class User implements UserDetails {

  private long id;
  private String userName;
  private String loginName;
  private String password;
  private long mobile;
  private long status;
  private long userType;
  private Date updateTime;
  private Date createTime;
  private Date lastLoginTime;
  private Date loginTime;
  private long loginCount;
  //权限
  private List<Role> roles;

  /**
   * 根据自定义逻辑来返回用户权限， 如果用户权限返回空或者和拦截路径对应权限不同，验证不通过
   */
  @Override
  public Collection<? extends GrantedAuthority> getAuthorities() {
    List<GrantedAuthority> authorities = new ArrayList<>();
    List<Role> roles = this.getRoles();
    if(!CollectionUtils.isEmpty(roles)){
      for (Role role : roles) {
        authorities.add(new SimpleGrantedAuthority(role.getRoleName()));
      }
    }
    return authorities;
  }

  @Override
  public String getUsername() {
    return this.userName;
  }

  /**
   * 帐号是否不过期，false则验证不通过
   */
  @Override
  public boolean isAccountNonExpired() {
    return true;
  }

  /**
   * 帐号是否不锁定，false则验证不通过
   */
  @Override
  public boolean isAccountNonLocked() {
    return true;
  }

  @Override
  public boolean isCredentialsNonExpired() {
    return true;
  }

  /**
   * 凭证是否不过期，false则验证不通过
   */
  @Override
  public boolean isEnabled() {
    return true;
  }
}
